package com.wd.mylibrary_base.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者: 邢凌志
 * 时间: 2024/9/12
 * 描述: Activity 基类
 */

abstract class BaseActivity<V: ViewModel,D: ViewDataBinding>(private val layoutId:Int) : AppCompatActivity() {



    lateinit var viewModel: V
    lateinit var dataBinding: D
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var type=javaClass.genericSuperclass as ParameterizedType
        var  classVM=type.actualTypeArguments[0]as Class<V>
        viewModel= ViewModelProvider(this)[classVM]

        dataBinding= DataBindingUtil.setContentView(this,layoutId)
        dataBinding.lifecycleOwner=this

        initView()
        dataObserve()
    }

    abstract fun initView()

    abstract fun dataObserve()
    override fun onDestroy() {
        super.onDestroy()
        if(dataBinding!=null){
            dataBinding.unbind()
        }
    }
}